package com.team.activity.service.impl;

import com.team.activity.domain.BizActivityLog;
import com.team.activity.mapper.BizActivityLogMapper;
import com.team.activity.service.WebSocketService;
import org.springframework.stereotype.Service;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import com.team.activity.mapper.BizActivityPushLogMapper;

@Service
public class WebSocketServiceImpl implements WebSocketService {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private BizActivityPushLogMapper activityPushLogMapper;
    @Autowired
    private BizActivityLogMapper activityLogMapper;

    @Override
    public void sendActivityNotification(Long userId, Long activityId) {
        // 推送未读消息数到指定用户
        String destination = "/topic/unread/" + userId;
        // 这里假设有方法getUnreadCountByUserId(userId)获取未读数
        int unreadCount = getUnreadCountByUserId(userId);
        messagingTemplate.convertAndSend(destination, unreadCount);
        BizActivityLog bizActivityLog = activityLogMapper.selectByPrimaryKey(activityId);
        if (bizActivityLog != null) {
            // 推送活动内容到指定用户
            String contentDestination = "/topic/activity/" + userId;
            messagingTemplate.convertAndSend(contentDestination, bizActivityLog);
        }
    }

    // 你需要实现此方法，统计该用户未读活动消息数
    private int getUnreadCountByUserId(Long userId) {
        // 查询BizActivityPushLog表，统计status=0的数量
        return activityPushLogMapper.countUnreadByUserId(userId);
    }
} 